class MakeController < ApplicationController
  def index
    @project = Project.new 
    @project.components << Component.new
    project_hash = @project.jsonize

    @payload = payload(project_hash)
    session[:project] = project_hash
  end

  def project
    # archive session
#    if session[:project][:id]
#      project = save_session_project
#      project.save
#    end

    # load project
    @project = Project.find(params[:id]) if params[:id]
    project_hash = @project.jsonize
    @payload = payload(project_hash)
    session[:project] = project_hash

    render :action => :index
  end

  def archive
    save_component(params[:currentIndex], params)
    session[:project][:name] = params[:name]

    project = save_session_project
    project.save

    render_ajax 'success'
  end

  def download
    save_component(params[:currentIndex], params)
    session[:project][:name] = params[:name]

    project = save_session_project

    render_ajax :redirect_to => project.zip
  end

  def new
    macro_name = params[:macro] || "blank"
    macro = Project::MACROS.find{ |m| m[:id] == macro_name.to_sym }
    macro[:editing] = 0

    session[:project] = macro
    render_ajax payload(macro)
  end

  def add
    session[:project] ||= Project.new_from_macro
    save_component(params[:currentIndex], params)

    insert_point = params[:id].to_i + 1
    session[:project][:components].insert(insert_point, Component.new.jsonize)
    session[:project][:editing] = insert_point

    render_ajax payload(session[:project])
  end

  def remove
    session[:project] ||= Project.new_from_macro
    current_index = params[:currentIndex].to_i
    remove_index = params[:id].to_i
    save_component(current_index, params) unless current_index == remove_index

    session[:project][:components].delete_at(remove_index)
    max_index = session[:project][:components].length - 1
    session[:project][:editing] = [remove_index, max_index].min

    render_ajax payload(session[:project])
  end
  
  def movedown
    session[:project] ||= Project.new_from_macro
    current_index = params[:currentIndex].to_i
    i = params[:id].to_i

    cs = session[:project][:components]
    cs[i +1], cs[i] = cs[i], cs[i +1] 
    
    session[:project][:editing] = i + 1 if current_index == i
    session[:project][:editing] = i if current_index == i + 1

    render_ajax payload(session[:project])
  end

  def moveup
    session[:project] ||= Project.new_from_macro
    current_index = params[:currentIndex].to_i
    i = params[:id].to_i

    cs = session[:project][:components]
    cs[i -1], cs[i] = cs[i], cs[i -1] if i > 0
    
    session[:project][:editing] = i - 1 if current_index == i
    session[:project][:editing] = i if current_index == i - 1

    render_ajax payload(session[:project])
  end

  def edit
    session[:project] ||= Project.new_from_macro
    save_component(params[:currentIndex], params)
    session[:project][:editing] = params[:id]

    render_ajax payload(session[:project])
  end

  def save
    session[:project] ||= Project.new_from_macro
    index = params[:currentIndex].to_i
    save_component(index, params)

    render_ajax true
  end

  include Filler

  def fill
    query = params[:q].strip
    raise "" if query.blank?

    ret = fill_verses(query) || fill_song(query) 
    raise "Found nothing" unless ret
    payload = {:resource => ret[0], :body => ret[1]}

    render_ajax payload
  rescue Exception => e
    add_error e.to_s
    render_ajax({:resource => "", :body => ""})
  end

  def find_resource
    query = params[:q].strip
    templates = Template.search(query) || Template.find(:all)

    render_ajax templates.collect(&:jsonize)
  end
  
  private
  def save_session_project
    project = Project.find(session[:project][:id]) rescue Project.new
    project.js_hash = session[:project]

    return project
  end
  
  def save_component(current_index, hash)
    com = Component.new
    com.js_hash= hash
    session[:project][:components][current_index.to_i] = com.jsonize
  end

  def payload(project_hash)
    payload = {}
    payload[:list] = []
    project_hash[:components].each_with_index do |m, i| 
      payload[:list].push({ :title => m[:title], 
        :pages => (m[:body] || ' ').split("\n\n").length,
        :index => i
      })
    end

    current_index = project_hash[:editing].to_i || 0
    payload[:list][current_index][:current] = true
    payload[:current] = project_hash[:components][current_index]
    payload[:name]    = project_hash[:name]
    payload[:id]      = project_hash[:id] if project_hash[:id]

    return payload
  end
end
